#include<memory.h>
#include<stdio.h>

void abilita_paging() {
	unsigned int control;
	asm volatile("mov %%cr0,%0":"=r"(control));
	control|=0x80000000;
	asm volatile("mov %0,%%cr0"::"r"(control));
}

void disabilita_paging() {
	unsigned int control;
	asm volatile("mov %%cr0,%0":"=r"(control));
	control^=0x80000000;
	asm volatile("mov %0,%%cr0"::"r"(control));
}

void init_paging() {
	long flags;
	save_eflag(flags);
	asm("cli");
	
	unsigned long *page_dir=(unsigned long*)0x1000;
	unsigned long *page_table=(unsigned long*)0x2000;
	unsigned long address=0,pd_addr;
	unsigned int i;
	
	for(i=0;i<1024;i++) {
		page_table[i]=address | KERNEL_PAGES_FLAG;	// <-- specificare KERNEL_PAGES_FLAG
		address=address+4096;
	}
	page_dir[0]=(unsigned long)page_table;
	page_dir[0]=page_dir[0] | KERNEL_PAGES_FLAG;		// <-- specificarlo anche qui

	for(i=1;i<1024;i++) page_dir[i]= 0 | PAG_READWRITE;
	page_dir[1023]=(unsigned long)page_dir;
	page_dir[1023]=page_dir[1023] | KERNEL_PAGES_FLAG;	// <-- idem
	pd_addr=(unsigned long int)page_dir&0xFFFFF000;
	asm volatile("movl %0,%%cr3"::"r"(pd_addr));
	abilita_paging();
	restore_eflag(flags);
}
